﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using Jalal.Library.DataStructure;
using Jalal.Library.Utility;
using BCGC.BLL.Helper;
using Jalal.Library.Data;
using BCGC.DAL;

namespace BCGC.BLL.BusinessObject.UserAccount
{
    public class UserInfo
    {
        private int userId;
        public int UserId
        {
            get { return userId; }
            set { userId = value; }
        }



        protected String username;
        public String Username
        {
            get { return username; }
            set { username = value; }
        }

        protected String email;
        public String Email
        {
            get { return email; }
            set { email = value; }
        }

        protected bool isOnline;
        public bool IsOnline
        {
            get { return isOnline; }
            set { isOnline = value; }
        }

        protected DateTime lastLoginTime;
        public DateTime LastLoginTime
        {
            get { return lastLoginTime; }
            set { lastLoginTime = value; }
        }


        private String realName;
        public String RealName
        {
            get
            {
                return realName;
            }
            set
            {
                realName = value;
            }
        }

        private DateTime dateOfBirth;
        public DateTime DateOfBirth
        {
            get
            {
                return dateOfBirth;
            }
            set
            {
                dateOfBirth = value;
            }
        }

        private String messenger;
        public String Messenger
        {
            get
            {
                return messenger;
            }
            set
            {
                messenger = value;
            }
        }


        private String mood;
        public String Mood
        {
            get
            {
                return mood;
            }
            set
            {
                mood = value;
            }
        }


        private String avatarImageUrl;
        public String AvatarImageUrl
        {
            get
            {
                return avatarImageUrl;
            }
            set
            {
                avatarImageUrl = value;
            }
        }

        public String OnlineOfflineImageUrl
        {
            get
            {
                if (IsOnline) return SiteConstants.DefaultUserOnlineImage;
                else return SiteConstants.DefaultUserOfflineImage;
            }
            
        }

        private String useProfileImage;
        public String UseProfileImage
        {
            get
            {
                return useProfileImage;
            }
            set
            {
                useProfileImage = value;
            }
        }

        private string memberId;
        public string MemberId
        {
            get { return memberId; }
            set { memberId = value; }
        }

        private int totalPost;
        public int TotalPost
        {
            get { return totalPost; }
            set { totalPost = value; }
        }

        private int totalMarketPost;
        public int TotalMarketPost
        {
            get
            {
                return totalMarketPost;
            }
            set
            {
                totalMarketPost = value;
            }
        }
        public UserInfo(DataRow dataRow)
        {
            UserId = (int)CommonUtility.FilterNull(dataRow["UserId"], typeof(int));
            Username = (string)CommonUtility.FilterNull(dataRow["SiteUserName"], typeof(string));
            Email = (string)CommonUtility.FilterNull(dataRow["Email"], typeof(string));
            IsOnline = (bool)CommonUtility.FilterNull(dataRow["IsOnline"], typeof(bool));
            LastLoginTime = (DateTime)CommonUtility.FilterNull(dataRow["LastLoginTime"], typeof(DateTime));
            RealName = (string)CommonUtility.FilterNull(dataRow["RealName"], typeof(string));
            DateOfBirth = (DateTime)CommonUtility.FilterNull(dataRow["DateOfBirth"], typeof(DateTime));
            Messenger = (string)CommonUtility.FilterNull(dataRow["Messenger"], typeof(string));
            Mood = (string)CommonUtility.FilterNull(dataRow["Mood"], typeof(string));
            AvatarImageUrl = SiteUtility.FormateImage((string)CommonUtility.FilterNull(dataRow["AvatarImageUrl"], typeof(string)),ImageType.UserImage);
            UseProfileImage = SiteUtility.FormateImage((string)CommonUtility.FilterNull(dataRow["UseProfileImage"], typeof(string)),ImageType.UserImage);
            MemberId = (string)CommonUtility.FilterNull(dataRow["MemberId"], typeof(string));
            TotalPost = (int)CommonUtility.FilterNull(dataRow["TotalPost"], typeof(int));
            TotalMarketPost = (int)CommonUtility.FilterNull(dataRow["TotalMarketPost"], typeof(int));
        }

        private static ReadWriteCustomCollection<UserInfo> GetCollection(DataRowCollection rowCollection)
        {
            ReadWriteCustomCollection<UserInfo> collection = new ReadWriteCustomCollection<UserInfo>();

            foreach (DataRow dr in rowCollection)
            {
                collection.Add(new UserInfo(dr));
            }
            return collection;
        }

        public static UserInfo GetUserInfoByUserID(int userID)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = UserData.GetUserInfoByUserId(userID);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new UserInfo(result.Rows[0]);
            else
                return null;
        }

        public static UserInfo GetUserInfoByUserName(string userName)
        {
            DataFactory.GetDataMiner().BeginTransaction(true);
            DataTable result = UserData.GetUserInfoByUserName(userName);
            DataFactory.GetDataMiner().CommitTransaction();

            if (result.Rows.Count > 0)
                return new UserInfo(result.Rows[0]);
            else
                return null;
        }

        // Designed for web service
        public static ReadWriteCustomCollection<UserInfo> GetFriendsByName(string searchString,int userID,int count)
        {
            
            DataTable result = UserData.GetFriendsByLikeName(searchString, userID);
            

            if (result.Rows.Count > 0)
                return GetCollection(result.Rows);
            else
                return null;
        }


    }
}
